package grpc

import (
	"context"

	"gitee.com/zawei/yggdrasil/pkg/errors"
	"google.golang.org/grpc"
	"google.golang.org/grpc/status"
)

func errorsUnaryServerInterceptor(ctx context.Context, req interface{},
	info *grpc.UnaryServerInfo,
	handler grpc.UnaryHandler,
) (interface{}, error) {
	resp, err := handler(ctx, req)
	if err != nil {
		err = status.ErrorProto(errors.FromError(err).Proto())
	}
	return resp, err
}

func errorsStreamServerInterceptor(srv interface{}, stream grpc.ServerStream,
	info *grpc.StreamServerInfo,
	handler grpc.StreamHandler,
) error {
	if err := handler(srv, stream); err != nil {
		return status.ErrorProto(errors.FromError(err).Proto())
	}
	return nil
}
